/* eslint-disable */
/**
 * 从条码中获取附加信息
 * test:[)>TXH0616D2019.04.029D12MO24D360DA27D3YR28D12HR29D360HR30D2019.04.3112D2022.04.3114D2025.04.312Q12GR7C12HRQ303P一般1V12234569J8Vcgtest11K司采购-SCCG-20-0007-0018K司采购-SCCG-20-000712C这是附加信息
 * test:[)>/RS/TXH06/GS/16D2019.04.02/GS/9D12MO/GS/24D360DA/GS/27D3YR/GS/28D12HR/GS/29D360HR/GS/30D2019.04.31/GS/12D2022.04.31/GS/14D2025.04.31/GS/2Q12GR/GS/7C12HR/GS/Q30/GS/3P一般/GS/1V12234569J/GS/8Vcgtest1/GS/1K司采购-SCCG-20-0007-001/GS/8K司采购-SCCG-20-0007/GS/12C这是附加信息/RS//EOT/
 * @type {{}}
 */
import { Message } from '@arco-design/web-vue';

function formattedCode(val) {
  val = val.replaceAll('/RS/', String.fromCharCode(30));
  val = val.replaceAll('/GS/', String.fromCharCode(29));
  val = val.replaceAll('/EOT/', String.fromCharCode(4));
  return val;
}

var noteObj = {};

export function transitionAddInfo(value) {
  // TODO: 格式化输入
  value = formattedCode(value);
  var display = '';
  try {
    var noteValues = value.split(String.fromCharCode(30));
    if (noteValues.length != 3) {
      Message.warning({
        content: '附加信息错误，请联系系统管理员!',
        duration: 5 * 1000,
      });
      return false;
    }
    if (
      noteValues[0] != '[)>' ||
      noteValues[2].substr(0, 1) != String.fromCharCode(4)
    ) {
      Message.warning({
        content: '附加信息错误，请联系系统管理员!',
        duration: 5 * 1000,
      });
      return false;
    }
    var value = strReNew(noteValues[1]).split(String.fromCharCode(29));
    noteObj = {};
    for (var i = 0; i < value.length; i++) {
      if (value[i].substr(0, 1) == 'T') {
        display += '型号：' + value[i].substring(1, value[i].length) + '\n';
        noteObj.scanfModelNum = value[i].substring(1, value[i].length);
      } else if (value[i].substr(0, 3) == '16D') {
        display += '出厂日期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfProduceDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 2) == '9D') {
        display += '贮存期限：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfMemoryDate = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 3) == '24D') {
        display += '保证期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfSureDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '27D') {
        display += '寿命：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfLifeDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '28D') {
        display += '首翻期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfFirstDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '29D') {
        display += '使用期限：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfUseDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '30D') {
        display += '验收日期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfGetDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '12D') {
        display += '封存日期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfCloseDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 3) == '14D') {
        display += '失效日期：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfFailureDate = value[i].substring(3, value[i].length);
      } else if (value[i].substr(0, 2) == '2Q') {
        display += '实际重量：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfActualWeight = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '7C') {
        display += '储运条件：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfTransportCondition = value[i].substring(
          2,
          value[i].length
        );
      } else if (value[i].substr(0, 1) == 'Q') {
        display += '数量：' + value[i].substring(1, value[i].length) + '\n';
        noteObj.scanfQty = value[i].substring(1, value[i].length);
      } else if (value[i].substr(0, 2) == '3P') {
        display += '重要程度：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfImportSign = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '1V') {
        display +=
          '发货单位代码：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfFromUnit = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '8V') {
        display +=
          '收货单位代码：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfToUnit = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '1K') {
        display += '订单号：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfOrderNumber = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '8K') {
        display += '合同号：' + value[i].substring(2, value[i].length) + '\n';
        noteObj.scanfContractNumber = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 3) == '12C') {
        display += '其他：' + value[i].substring(3, value[i].length) + '\n';
        noteObj.scanfOtherInfo = value[i].substring(2, value[i].length);
      } else if (value[i].substr(0, 2) == '21') {
      } else {
        Message.warning({
          content: '附加信息解析失败，请联系系统管理员!',
          duration: 5 * 1000,
        });
        return false;
      }
    }
    //显示
    return {
      display,
      noteObj,
    };
  } catch (e) {
    Message.warning({
      content: '附加信息解析失败，请联系系统管理员!',
      duration: 5 * 1000,
    });
    return false;
  }
}
function strReNew(string1) {
  if (string1 == '') {
    return '';
  } else {
    //替换
    return string1
      .replace(/YR/g, '年')
      .replace(/MO/g, '月')
      .replace(/DA/g, '天')
      .replace(/HR/g, '小时')
      .replace(/CZUD/g, '起落次数')
      .replace(/CZWK/g, '工作次数')
      .replace(/CZSS/g, '起动机起动次数')
      .replace(/KM/g, '千米')
      .replace(/MI/g, '英里')
      .replace(/NM/g, '海里')
      .replace(/MP/g, '吨')
      .replace(/KG/g, '千克')
      .replace(/GR/g, '克');
  }
}

//------------解析扫描的条码----------------
/**
 * 转换barcode,从条码中获取barcode
 * @param barcodeS
 * @returns {string}
 * test:[)>21M131301W50000010Scs--12
 * test:[)>/RS/21/GS/M131301/GS/W50000010/GS/Scs--12/RS//EOT/
 * test:[)>\\x1E21\\x1DM131301\\x1DW50000010\\x1DScs--12\\x1E\\x04
 * test:[)>/RS/x1E21\x1EM131301\x1EW50000010\x1EScs--12
 */
export function transitionBarcode(barcodeS) {
  try {
    // TODO: 格式化输入
    barcodeS = formattedCode(barcodeS);
    var codes = [];
    var fString = barcodeS.split(String.fromCharCode(30));
    if (fString.length != 3) {
      return '';
    }
    if (fString[0] != '[)>' || fString[2] != String.fromCharCode(4)) {
      return '';
    }
    var sString = fString[1].split(String.fromCharCode(29));
    if (sString.length != 4) {
      return '';
    }
    if (
      (sString[0] != '21' ||
        sString[1].charAt(0) != 'M' ||
        sString[2].charAt(0) != 'P' ||
        sString[3].charAt(0) != 'S') &&
      (sString[0] != '21' ||
        sString[1].charAt(0) != 'M' ||
        sString[2].charAt(0) != 'W' ||
        sString[3].charAt(0) != 'S')
    ) {
      return '';
    }
    return sString[1] + '!#!' + sString[2] + '!#!' + sString[3];
  } catch (e) {
    return '';
  }
}

/**
 * 判断扫描后的编码是否合规，并根据编码规则向不同输入框输入数据
 * @param scanCode
 * @param selfNode 当前node
 */
export function valiScanCodeAndSetInput(
  scanCode,
  selfNode,
  callBack,
  getValue
) {
  // TODO: 格式化输入
  scanCode = formattedCode(scanCode);

  var resultFlag = ''; //校验结果：-1，条码数据解析失败；1，三码合一；2，附加码；
  if (scanCode != '') {
    //条码不为空才可以解析数据
    var fString = scanCode.split(String.fromCharCode(30));
    if (fString.length != 3) {
      resultFlag = '-1';
    } else if (fString[0] != '[)>') {
      resultFlag = '-1';
    }
    //验证是否是三码合一
    if (resultFlag != '-1') {
      var sString = fString[1].split(String.fromCharCode(29));
      if (
        sString.length != 4 ||
        ((sString[0] != '21' ||
          sString[1].charAt(0) != 'M' ||
          sString[2].charAt(0) != 'P' ||
          sString[3].charAt(0) != 'S') &&
          (sString[0] != '21' ||
            sString[1].charAt(0) != 'M' ||
            sString[2].charAt(0) != 'W' ||
            sString[3].charAt(0) != 'S'))
      ) {
        resultFlag = '2';
        if (selfNode != 'barcodeNote') {
          callBack('setInput', selfNode, scanCode);
        }
        callBack('setInput', 'barcodeNote', scanCode);
      } else {
        resultFlag = '1';
        if (selfNode == 'barcodeNote') {
          callBack('setInput', selfNode, '');
          if (!getValue('barcodeOutPack')) {
            callBack('setInput', 'barcodeOutPack', scanCode);
          } else if (!getValue('barcodeResume')) {
            callBack('setInput', 'barcodeResume', scanCode);
          } else if (!getValue('barcodeEntity')) {
            callBack('setInput', 'barcodeEntity', scanCode);
          }
        }
      }
      callBack('setScanCodeObjFocus');
    } else {
      Message.warning({
        content: '条码数据解析失败!',
        duration: 5 * 1000,
      });
      callBack('setInput', selfNode, '');
    }
  }
  return resultFlag;
}

/**
 * 判断条码是否可以提交
 * @param selfNode 当前node
 * @returns {boolean}
 */
export function valiScanCodeAutoSubmit(selfNode, callBack, getValue) {
  var barcodeOutPackVal = getValue('barcodeOutPack');
  var barcodeResumeVal = getValue('barcodeResume');
  var barcodeEntityVal = getValue('barcodeEntity');
  var barcodeNoteVal = getValue('barcodeNote');
  if (
    barcodeOutPackVal &&
    barcodeResumeVal &&
    barcodeOutPackVal != barcodeResumeVal
  ) {
    Message.warning({
      content: '外包装条码和履历本条码不一致!',
      duration: 5 * 1000,
    });
    callBack('setInput', 'barcodeOutPack', '');
    callBack('setInput', 'barcodeResume', '');
    callBack('setFocus', 'barcodeOutPack');
  }
  if (
    barcodeOutPackVal &&
    barcodeEntityVal &&
    barcodeOutPackVal != barcodeEntityVal
  ) {
    Message.warning({
      content: '外包装条码和实物表面条码不一致!',
      duration: 5 * 1000,
    });
    callBack('setInput', 'barcodeOutPack', '');
    callBack('setInput', 'barcodeEntity', '');
    callBack('setFocus', 'barcodeOutPack');
  }
  if (
    barcodeResumeVal &&
    barcodeEntityVal &&
    barcodeResumeVal != barcodeEntityVal
  ) {
    Message.warning({
      content: '履历本条码和实物表面条码不一致!',
      duration: 5 * 1000,
    });
    callBack('setInput', 'barcodeResume', '');
    callBack('setInput', 'barcodeEntity', '');
    callBack('setFocus', 'barcodeResume');
  }
  if (barcodeNoteVal && !transitionAddInfo(barcodeNoteVal)) {
    callBack('setInput', 'barcodeNote', '');
    callBack('setInput', 'noteDisplay', '');
    callBack('setFocus', 'barcodeNote');
  }
  if (
    barcodeOutPackVal &&
    barcodeResumeVal &&
    barcodeEntityVal &&
    (selfNode == 'barcodeNote' || barcodeNoteVal)
  ) {
    //三类条码不为空，附加码为当前回车事件触发的对象时，提交数据到后台
    // insert4Barcode();
    callBack('saveData');
  }
}
